﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace DemoAstarAlgorithm {
    class ManagerMap {
        private Texture2D m_tPlay1, m_tPlay2, m_tPlay3; //Hinh nen cac man choi
        private Texture2D m_tSourceMap, m_tSourceItem;  //Hinh chuong ngai va vat pham
        private Texture2D m_tStatusGame;                //Hinh trang thai Game
        public int m_iIDStatus;                         //Chi so Frame cua hinh trang thai Game
        public int m_iTimeStatus;                       //Bien dem thoi gian trang thai
        private Texture2D m_tNo;                        //Hinh dem so trang thai     
        public int m_iChangePosItem;
        private int m_iDeltaPosItem;
        public cmap map = new cmap();
        #region CatHinh
        public Rectangle m_r2 = new Rectangle(0, 0, 40, 80);//cay
        public Rectangle m_r3 = new Rectangle(40, 0, 40, 80);//nam
        public Rectangle m_r4 = new Rectangle(80, 0, 40, 80);//hoa
        public Rectangle m_r5 = new Rectangle(200, 0, 40, 80);//bang
        public Rectangle m_r12 = new Rectangle(120, 0, 40, 80);//da
        public Rectangle m_r13 = new Rectangle(160, 0, 40, 80);//goc cay
        public Rectangle m_r14 = new Rectangle(240, 0, 40, 80);//cay thong
        public Rectangle m_r15 = new Rectangle(280, 0, 40, 80);//nguoi tuyet

        public Rectangle m_rStatusPrepare = new Rectangle(0, 0, 440, 120);
        public Rectangle m_rStatusBegin = new Rectangle(0, 120, 440, 120);
        public Rectangle m_rStatusLose = new Rectangle(0, 240, 440, 120);
        public Rectangle m_rStatusWin = new Rectangle(0, 360, 440, 120);
        public Rectangle m_rNo3 = new Rectangle(0, 0, 120, 120);
        public Rectangle m_rNo2 = new Rectangle(120, 0, 120, 120);
        public Rectangle m_rNo1 = new Rectangle(240, 0, 120, 120);
        #endregion
        public ManagerMap(ContentManager content) {
            m_iDeltaPosItem = 1;
            m_iChangePosItem = 2;
            m_iIDStatus = 1;
            m_iTimeStatus = 0;
            m_tPlay1 = content.Load<Texture2D>(@"images/bgplay1");
            m_tPlay2 = content.Load<Texture2D>(@"images/bgplay2");
            m_tPlay3 = content.Load<Texture2D>(@"images/bgplay3");
            m_tSourceMap = content.Load<Texture2D>(@"images/sourcemap");
            m_tSourceItem = content.Load<Texture2D>(@"images/item");
            m_tStatusGame = content.Load<Texture2D>(@"images/ttgame");
            m_tNo = content.Load<Texture2D>(@"images/so");
        }
        public int[, ,] getMapMatrix() {
            return map.m_aMatrix;
        }
        public void Update(GameTime gameTime, Vector2 pwitch) {
            map.m_i1 = 0;
            map.m_i2 = 0;
            map.m_i3 = 0;
            map.m_i4 = 0;
            map.m_i5 = 0;
            map.m_i12 = 0;
            map.m_i13 = 0;
            map.m_i14 = 0;
            map.m_i15 = 0;
            for (map.m_iRow = 0; map.m_iRow < 13; map.m_iRow++)
                for (map.m_iColumn = 0; map.m_iColumn < 15; map.m_iColumn++) {
                    switch (map.m_aMatrix[map.IDmap - 1, map.m_iRow, map.m_iColumn]) {
                        case 0:
                            break;
                        case 1:
                            map.m_i1++;
                            break;
                        case 2:
                            map.m_i2++;
                            break;
                        case 3:
                            map.m_i3++;
                            break;
                        case 4:
                            map.m_i4++;
                            break;
                        case 5:
                            map.m_i5++;
                            break;
                        case 12:
                            map.m_i12++;
                            break;
                        case 13:
                            map.m_i13++;
                            break;
                        case 14:
                            map.m_i14++;
                            break;
                        case 15:
                            map.m_i15++;
                            break;
                        default:
                            break;
                    }
                }
            map.m_iTotal = map.m_i2 + map.m_i3 + map.m_i4 + map.m_i5 + map.m_i12 + map.m_i13 + map.m_i14 + map.m_i15;
            if ((m_iChangePosItem == 2) || (m_iChangePosItem == -2))
                m_iDeltaPosItem *= -1;
            if (m_iChangePosItem > 2)
                m_iDeltaPosItem = -1;
            if (m_iChangePosItem < -2)
                m_iDeltaPosItem = 1;
            map.m_tsTime += gameTime.ElapsedGameTime;
            if (map.m_tsTime > TimeSpan.FromMilliseconds(90)) {
                map.m_tsTime = TimeSpan.Zero;
                m_iChangePosItem += m_iDeltaPosItem;
            }
        }
        public void Update(StoreObj[] listObj, GameTime gameTime, cManagerItem listItem, cboom[] boomGame) {
            foreach (StoreObj obj in listObj)
                foreach (cboom Boom in boomGame) {
                    if (obj.m_bAlive && Boom.m_bAlive == false && obj.m_v2Position == Boom.m_v2Position) {
                        switch (obj.m_iIDBoom) {
                            case 1:
                                listItem.InitItem(map.m_aMatrix, map.IDmap - 1, obj.m_iRow, obj.m_iColumn);
                                obj.m_v2Position = new Vector2(-1, -1);
                                obj.m_bAlive = false;
                                obj.m_iRow = 0;
                                obj.m_iColumn = 0;
                                obj.m_iIDBoom = 0;
                                break;
                            default:
                                if (obj.m_iIDBoom > 0)
                                    obj.m_iIDBoom--;
                                break;
                        }
                    }
                }
        }
        public void Draw(SpriteBatch spriteBatch) {
            if (map.IDmap == 1)
                spriteBatch.Draw(m_tPlay1, new Vector2(), null, Color.LightGray, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            else
                if (map.IDmap == 2)
                    spriteBatch.Draw(m_tPlay2, new Vector2(), null, Color.LightGray, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                else
                    spriteBatch.Draw(m_tPlay3, new Vector2(), null, Color.LightGray, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
        }
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime, cTime time) {
            if (Game1.g_bPause) {
                switch (m_iIDStatus) {
                    case 1:
                        m_iTimeStatus += gameTime.ElapsedGameTime.Milliseconds;
                        if (m_iTimeStatus < 1000) {
                            spriteBatch.Draw(m_tStatusGame, new Vector2(100, 220), m_rStatusPrepare, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.9f);
                            spriteBatch.Draw(m_tNo, new Vector2(250, 260), m_rNo3, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                        } else
                            if (m_iTimeStatus < 2000) {
                                spriteBatch.Draw(m_tStatusGame, new Vector2(100, 220), m_rStatusPrepare, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.9f);
                                spriteBatch.Draw(m_tNo, new Vector2(250, 260), m_rNo2, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                            } else {
                                m_iTimeStatus = 0;
                                m_iIDStatus = 2;
                            }
                        break;
                    case 2:
                        m_iTimeStatus += gameTime.ElapsedGameTime.Milliseconds;
                        if (m_iTimeStatus < 1000) {
                            spriteBatch.Draw(m_tStatusGame, new Vector2(100, 220), m_rStatusBegin, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.9f);
                            spriteBatch.Draw(m_tNo, new Vector2(250, 260), m_rNo1, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                        } else
                            if (time.m_iMinute == 3)
                                Game1.g_bPause = false;
                        break;
                    case 3:
                        m_iTimeStatus += gameTime.ElapsedGameTime.Milliseconds;
                        if (Game1.g_bPause && m_iTimeStatus < 2000) {
                            spriteBatch.Draw(m_tStatusGame, new Vector2(100, 200), m_rStatusLose, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.9f);
                        } else {
                            Game1.g_bMenusRunning = true;
                            Game1.g_bEndGame = true;
                        }
                        break;
                    case 4:
                        if (time.m_iSecond == 0 && time.m_iMinute == 0) {
                            m_iTimeStatus += gameTime.ElapsedGameTime.Milliseconds;
                        }
                        if (Game1.g_bPause && m_iTimeStatus < 2000)
                            spriteBatch.Draw(m_tStatusGame, new Vector2(100, 200), m_rStatusWin, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.9f);
                        if (m_iTimeStatus > 2000) {
                            if (map.IDmap != 3) {
                                map.IDmap = 3;
                                m_iTimeStatus = 0;
                                m_iIDStatus = 1;
                            } else {
                                Game1.g_bMenusRunning = true;
                                Game1.g_bEndGame = true;
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime, cManagerItem listItem) {
            float m_fBGLayerDepth;
            #region LoadMap
            for (map.m_iRow = 0; map.m_iRow < 13; map.m_iRow++)
                for (map.m_iColumn = 0; map.m_iColumn < 15; map.m_iColumn++) {
                    m_fBGLayerDepth = (40 * map.m_iRow + 35) / 600.0f;
                    switch (map.m_aMatrix[map.IDmap - 1, map.m_iRow, map.m_iColumn]) {
                        case 2:
                            spriteBatch.Draw(m_tSourceMap, new Vector2(20 + 40 * (map.m_iColumn), 40 * (map.m_iRow)), m_r2, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        case 3:
                            spriteBatch.Draw(m_tSourceMap, new Vector2(20 + 40 * (map.m_iColumn), 40 * (map.m_iRow)), m_r3, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        case 4:
                            spriteBatch.Draw(m_tSourceMap, new Vector2(20 + 40 * (map.m_iColumn), 40 * (map.m_iRow)), m_r4, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        case 5:
                            spriteBatch.Draw(m_tSourceMap, new Vector2(20 + 40 * (map.m_iColumn), 40 * (map.m_iRow)), m_r5, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        case 6:
                            break;
                        case 7:
                            listItem.Draw(spriteBatch, map.m_iColumn, map.m_iRow, 7, gameTime);
                            break;
                        case 8:
                            listItem.Draw(spriteBatch, map.m_iColumn, map.m_iRow, 8, gameTime);
                            break;
                        case 9:
                            listItem.Draw(spriteBatch, map.m_iColumn, map.m_iRow, 9, gameTime);
                            break;
                        case 10:
                            listItem.Draw(spriteBatch, map.m_iColumn, map.m_iRow, 10, gameTime);
                            break;
                        case 12:
                            spriteBatch.Draw(m_tSourceMap, new Vector2(20 + 40 * (map.m_iColumn), 40 * (map.m_iRow)), m_r12, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        case 13:
                            spriteBatch.Draw(m_tSourceMap, new Vector2(20 + 40 * (map.m_iColumn), 40 * (map.m_iRow)), m_r13, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        case 14:
                            spriteBatch.Draw(m_tSourceMap, new Vector2(20 + 40 * (map.m_iColumn), 40 * (map.m_iRow)), m_r14, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        case 15:
                            spriteBatch.Draw(m_tSourceMap, new Vector2(20 + 40 * (map.m_iColumn), 40 * (map.m_iRow)), m_r15, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, m_fBGLayerDepth);
                            break;
                        default:
                            break;
                    }
                }

            #endregion
        }
    }

    class cmap {
        public int m_iColumn, m_iRow;
        public int[, ,] m_aMatrix =
        {
        {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,13,0,12,0,13,0,0,0,0,0},
        {0,0,0,0,0,0,12,0,12,0,0,0,0,0,0},
        {0,0,0,13,0,12,0,0,0,12,0,13,0,0,0},
        {0,0,0,0,0,0,2,12,2,0,0,0,0,0,0},
        {0,13,0,13,0,13,2,12,2,13,0,13,0,13,0},
        {0,0,3,0,3,2,0,12,0,2,4,0,4,0,0},
        {0,13,3,0,3,0,2,12,2,0,4,0,4,13,0},
        {0,0,3,0,3,3,0,12,0,4,4,0,4,0,0},
        {0,13,0,13,2,3,0,12,0,4,2,13,0,13,0},
        {0,0,0,0,2,0,0,12,0,0,2,0,0,0,0},
        {0,0,0,0,0,13,0,12,0,13,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
        },
        {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,15},
        {0,0,15,5,5,0,0,5,15,5,14,5,15,0,0},
        {0,0,5,0,15,0,0,5,0,0,0,0,5,0,0},
        {0,0,14,0,5,0,0,5,0,15,5,15,5,0,0},
        {0,0,5,0,15,5,5,14,0,5,0,0,0,0,5},
        {0,0,15,0,0,0,0,0,0,5,0,0,0,0,14},
        {0,0,5,5,5,14,0,0,0,14,5,5,5,0,0},
        {14,0,0,0,0,5,0,0,0,0,0,0,15,0,0},
        {5,0,0,0,0,5,0,14,5,5,15,0,5,0,0},
        {0,0,5,15,5,15,0,5,0,0,5,0,14,0,0},
        {0,0,5,0,0,0,0,5,0,0,15,0,5,0,0},
        {0,0,15,5,14,5,15,5,0,0,5,5,15,0,0},
        {15,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
        },
        {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,14,12,14,14,12,14,14,12,3,14,12,0,0},
        {0,0,14,0,0,3,4,2,4,14,0,0,14,0,0},
        {0,0,12,0,0,3,0,0,0,14,0,0,14,0,0},
        {0,0,3,0,0,14,0,0,0,12,0,0,2,0,0},
        {0,0,3,0,0,14,0,0,0,4,0,0,14,0,0},
        {0,0,2,0,0,12,0,0,0,4,0,0,14,0,0},
        {0,0,14,0,0,14,0,0,0,3,0,0,12,0,0},
        {0,0,14,0,0,14,4,2,4,3,0,0,14,0,0},
        {0,0,12,14,3,12,14,14,12,14,14,12,14,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
        }
        };
        public int IDmap = 1;

        public TimeSpan m_tsTime;
        public int m_i2 = 0;
        public int m_i3 = 0;
        public int m_i4 = 0;
        public int m_i5 = 0;
        public int m_i12 = 0;
        public int m_i13 = 0;
        public int m_i14 = 0;
        public int m_i15 = 0;
        public int m_iTotal = 0;
        public int m_i1 = 0;
        public cmap() {
            for (m_iRow = 0; m_iRow < 13; m_iRow++)
                for (m_iColumn = 0; m_iColumn < 15; m_iColumn++) {
                    switch (m_aMatrix[IDmap - 1, m_iRow, m_iColumn]) {
                        case 0:
                            break;
                        case 1:
                            break;
                        case 2:
                            m_i2++;
                            break;
                        case 3:
                            m_i3++;
                            break;
                        case 4:
                            m_i4++;
                            break;
                        case 5:
                            m_i5++;
                            break;
                        case 12:
                            m_i12++;
                            break;
                        case 13:
                            m_i13++;
                            break;
                        case 14:
                            m_i14++;
                            break;
                        case 15:
                            m_i15++;
                            break;
                        default:
                            break;
                    }
                }
            m_iTotal = m_i2 + m_i3 + m_i4 + m_i5 + m_i12 + m_i13 + m_i14 + m_i15;
            m_tsTime = TimeSpan.Zero;
        }
    }
}